package aufgabe3.permutations
import scala.annotation.tailrec

class Permutations(val permut: Int*) {

  override def equals(in: Any): Boolean = { // ==
    in match {
      case k: Permutations if (k.permut == permut) => true
      case _ => false
    }
  }

  def /\/\(index: Int) = { // ()
    permut(index)
  }

  def fixPoints() = {
    @tailrec
    def fix(i: Int, count: Int*): List[Int] = {
      if (i < 5) {
        if (i + 1 == permut(i)) {
          fix(i + 1, (count :+ (i + 1)): _*)
        } else
          fix(i + 1, count: _*)
      } else
        count.toList
    }

    if (permut.length == 5) {
      fix(0)
    }
  }

  def cycles() {   
    @tailrec
    def _cycles(i: Int, search: Int, hops: Int*): List[Int] = {
      if (i < 5) {
        if (permut(i) != search)
          _cycles(i + 1, search, (hops :+ permut(i)): _*)
        else
          (hops :+ permut(i)).toList
      }else
        List[Int]()
    }
    
    _cycles(0, permut(0))
  }
}

object Permutations {
  def apply(e: Int*): Permutations = {
    if (e.length != 5) {
    } else {
      val list = List[Int](e(0), e(1), e(2), e(3), e(4))
      for (i <- List(1, 2, 3, 4, 5).permutations) {
        if (i == list) {
          return new Permutations(e: _*)
        }
      }
    }
    new Permutations()
  }
}